 /**
 * Description: 物资管理
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.biz.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.biz.mapper.IBizMaterialsManageMapper;
import com.metro.paperless.biz.service.inf.IBizMaterialsManageService;
import com.metro.paperless.biz.beans.vo.BizMaterialsManage;
import com.metro.paperless.biz.beans.vo.BizMaterialsSource;

 /**
 * 物资管理<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Service("bizmaterialsmanageService")
public class BizMaterialsManageService extends BaseServiceSupport<BizMaterialsManage> implements IBizMaterialsManageService{
    //物资管理Mapper
    @Autowired
    private IBizMaterialsManageMapper bizmaterialsmanageMapper;

    @Override
    public IMapper getMapper() {
        return bizmaterialsmanageMapper;
    }

    @Override
    public IResult save(BizMaterialsManage entity) {
        try {
            bizmaterialsmanageMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资管理失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(BizMaterialsManage entity) {
        try {
            bizmaterialsmanageMapper.updateNotEmpty(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改物资管理失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            bizmaterialsmanageMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            bizmaterialsmanageMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    
    
    /**
     * 判断物资是否已存在
     */
    public IResult checkMaterials(BizMaterialsManage entity) {
    	//判断是否已存在
    	List<BizMaterialsManage> list = bizmaterialsmanageMapper.queryForList();
    	if(list != null && list.size()>0 ) {
    		for(int i=0;i<list.size();i++){
    			//物资名称、类别、和规格相同判定为同一种物资
        		if(list.get(i).getMaterialsName().equals(entity.getMaterialsName()) && list.get(i).getSpecification().equals(entity.getSpecification()) && list.get(i).getMaterialsType().equals(entity.getMaterialsType()) && list.get(i).getDataStatus() == 1) {
        			return ResultCode.DUPLICATE_KEY;
        		}
        	}
    	}
    	return ResultCode.SUCCESS;
    }
    
    /**
     * 修改时判断物资是否已存在
     */
    public IResult checkUpdateMaterials(BizMaterialsManage entity) {
    	List<BizMaterialsManage> list = bizmaterialsmanageMapper.queryForList();
    	if(list != null && list.size()>0 ) {
    		for(int i=0;i<list.size();i++){
    			//修改时判断数据是否改动过
    			if(list.get(i).getMaterialsName().equals(entity.getMaterialsName()) && list.get(i).getSpecification().equals(entity.getSpecification()) && list.get(i).getMaterialsType().equals(entity.getMaterialsType()) && list.get(i).getUnit().equals(entity.getUnit()) && list.get(i).getCurStatus().equals(entity.getCurStatus()) && list.get(i).getDataStatus() == 1 ) {
    				return ResultCode.DUPLICATE_KEY;
    			}
    		}
    	}
    	return ResultCode.SUCCESS;
    }
    
    
    
    /**
     * 新增物资管理
     */
    @Override
    public IResult saveBizMaterials(BizMaterialsManage entity) {
    	//添加时间
    	Date date = new Date();
    	entity.setAddTime(date);
    	//物资编号自增
    	BizMaterialsManage entity1 = bizmaterialsmanageMapper.queryTopList();
    	String code;
    	
    	if(entity1 != null) {
	    	code = entity1.getMaterialsCode();
	    	String[] sCode = code.split("\\D");
		    int num = Integer.parseInt(sCode[2]);
		    num = num+1;
		    
		    if(num<10) {
		    	code = "MA000"+num;
		    }else if(num<100) {
		    	code = "MA00"+num;
		    }else if(num<1000) {
		    	code = "MA0"+num;
		    }else {
		   		code = "MA"+num;
		    }
    	}else {
    		code = "MA0001";
    	}   	
    	entity.setMaterialsCode(code);
    	
        try {
            bizmaterialsmanageMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资管理失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    
    /**
     * 修改物资管理
     */
    @Override
    public IResult updateMaterials(BizMaterialsManage entity) {
 
        try {
            bizmaterialsmanageMapper.updateNotEmpty(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改物资管理失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    
}